<template>
  <page-header-wrapper>
    <a-card :bordered="false">
      <div>
        <a-tabs v-model="activeTab" @change="tabChange">
          <a-tab-pane v-for="item in tabs" :key="item.value" :tab="item.text"></a-tab-pane>
        </a-tabs>
      </div>
      <div class="table-page-search-box">
        <a-form layout="inline">
          <a-row :gutter="24">
            <a-col :md="8" :sm="24">
              <a-form-item label="关键字">
                <div class="flex">
                  <a-input placeholder="请输入关键字" v-model="queryParam.name" allowClear></a-input>
                  <a-button type="primary" @click="getList()">查询</a-button>
                </div>
              </a-form-item>
            </a-col>
          </a-row>
        </a-form>
      </div>
    </a-card>

    <a-card :border="false">
      <div class="table-operator" style="display: flex; justify-content: space-between">
        <div></div>

        <a-button type="primary" @click="visible = true">添加</a-button>
      </div>

      <a-table
        size="default"
        :columns="columns"
        :data-source="list"
        :row-key="(record) => record.id"
        :pagination="{
          total: this.total,
          pageSize: this.pageSize,
          showSizeChanger: false,
          onChange: this.tablePageChange,
        }"
      >
        <span slot="created_at" slot-scope="text">
          <span> {{ text > 0 ? $moment(text * 1000).format('YYYY-MM-DD HH:mm:ss') : '-' }}</span>
        </span>

        <span slot="isDefault" slot-scope="text, record">
          <a-switch :checked="record.is_default === '1' ? true : false" @change="foodExtGoodsDefault(record)" />
        </span>

        <span slot="cover_pic" slot-scope="text">
          <div v-if="text" class="goods_pic_item_box">
            <div class="upload-img-box">
              <img :src="text" alt="" />
              <div class="action-hover">
                <div class="hover-bg"></div>
                <div class="hover-body">
                  <div class="action-icons">
                    <a-icon style="margin-right: 0" @click="previewPic(text)" type="eye" />
                  </div>
                </div>
              </div>
            </div>
          </div>
          <span v-else>-</span>
        </span>

        <span slot="action" slot-scope="text, record">
          <a-button type="link" size="small" @click="handleItem('edit', record)">编辑</a-button>
          <a-button type="link" size="small" @click="handleItem('remove', record)">删除</a-button>
        </span>
      </a-table>

      <a-modal :width="1000" v-model="visible" :title="getTitle" :footer="null">
        <a-form-model ref="ruleForm" :model="form" :rules="rules" :label-col="labelCol" :wrapper-col="wrapperCol">
          <a-form-model-item label="名称" prop="name">
            <a-input v-model="form.name" allowClear />
          </a-form-model-item>
          <a-form-model-item label="描述" prop="desc">
            <a-input v-model="form.desc" allowClear />
          </a-form-model-item>
          <a-form-model-item label="图片" prop="cover_pic">
            <cyyUploadImg
              imgSizeText="建议尺寸：100*100"
              @uploadSuccess="(e) => (form.cover_pic = e.data)"
              @deleteImg="form.cover_pic = ''"
              :imgSrc="form.cover_pic"
              :showSize="false"
            >
            </cyyUploadImg>
          </a-form-model-item>
          <a-form-item label="小料列表">
            <a-button type="primary" @click="addList()">增加一条小料</a-button>

            <div class="table">
              <div class="table-body">
                <a-row class="title self-th">
                  <a-col :span="5" style="text-align: left"> 小料名称</a-col>
                  <a-col :span="5" style="text-align: left"> 小料价格 </a-col>
                  <a-col :span="5" style="text-align: left"> 小料货号 </a-col>
                  <a-col :span="5" style="text-align: left"> 小料图片 </a-col>
                </a-row>
                <div class="list" @dragover.prevent>
                  <a-row
                    class="item tr-row"
                    v-for="(item, index) in form.attr"
                    :key="index"
                    :draggable="true"
                    v-dragging="{
                      item: item,
                      list: form.attr,
                      group: 'row',
                    }"
                  >
                    <a-col :span="5" style="text-align: left">
                      <a-input style="width: 100%" v-model="item.name"></a-input>
                    </a-col>
                    <a-col :span="5" style="text-align: left">
                      <a-input-number :precision="2" style="width: 100%" v-model="item.price" :min="0" />
                    </a-col>
                    <a-col :span="5" style="text-align: left">
                      <a-input style="width: 100%" v-model="item.no"></a-input>
                    </a-col>
                    <a-col :span="5" style="text-align: left">
                      <div class="guige-image-box flex-x-center">
                        <div v-if="item.pic" class="goods_pic_item_box">
                          <div class="upload-img-box">
                            <img :src="item.pic" alt="" />
                            <div class="action-hover">
                              <div class="hover-bg"></div>
                              <div class="hover-body">
                                <div class="action-icons">
                                  <a-icon @click="previewPic(item.pic)" type="eye" />

                                  <a-icon @click="item.pic = ''" type="delete" />
                                </div>
                              </div>
                            </div>
                          </div>
                        </div>
                        <div class="upload-box">
                          <div class="upload-content">
                            <a-icon style="font-size: 14px; color: rgba(0, 0, 0, 0.45)" type="plus" />
                            <span style="font-size: 12px"></span>
                          </div>
                          <div class="self-com">
                            <storage-select
                              @onOk="(e) => (item.pic = e)"
                              selectNum="1"
                              :defaultValue="item.pic"
                            ></storage-select>
                          </div>
                        </div>
                      </div>
                    </a-col>
                    <a-col :span="4" style="text-align: left">
                      <a-button type="danger" size="small" @click="form.attr.splice(index, 1)">删除</a-button>
                      <a-icon type="unordered-list" style="cursor: move; margin-left: 5px" />
                    </a-col>
                  </a-row>
                </div>
              </div>
            </div>
          </a-form-item>
          <div class="btn">
            <a-button type="primary" :disabled="disabled" @click="handleOk">提交</a-button>
            <a-button style="margin-left: 10px" @click="visible = false">取消</a-button>
          </div>
        </a-form-model>
      </a-modal>

      <a-modal
        :visible="showPreviwModal"
        @ok="
          () => {
            showPreviwModal = fasle
          }
        "
        @cancel="
          () => {
            showPreviwModal = fasle
          }
        "
      >
        <div style="display: flex; justify-content: center">
          <img style="width: 300px" :src="previewImgSrc" alt="" />
        </div>
      </a-modal>
    </a-card>
  </page-header-wrapper>
</template>

<script>
import { cyyUploadImg } from '@/components/CyyUi/index'
import { foodExtGoodsSave, foodExtGoodsList, foodExtGoodsDel, foodExtGoodsDefault } from '@/api/orderMeal'

const columns = [
  {
    title: 'ID',
    dataIndex: 'id',
    width: 100,
  },
  {
    title: '名称',
    dataIndex: 'name',
  },
  {
    title: '描述',
    dataIndex: 'desc',
  },
  {
    title: '图片',
    dataIndex: 'cover_pic',
    scopedSlots: {
      customRender: 'cover_pic',
    },
  },
  {
    title: '是否默认',
    scopedSlots: {
      customRender: 'isDefault',
    },
  },
  {
    title: '创建日期',
    dataIndex: 'created_at',
    scopedSlots: {
      customRender: 'created_at',
    },
    width: 150,
  },
  {
    title: '操作',
    scopedSlots: {
      customRender: 'action',
    },
    width: 150,
  },
]
export default {
  components: {
    cyyUploadImg,
  },
  data() {
    return {
      activeTab: '6',
      tabs: [
        {
          value: '0',
          text: '商品管理',
          path: 'OrderMealGoods',
        },
        {
          value: '1',
          text: '分类',
          path: 'OrderMealClassify',
        },
        {
          value: '2',
          text: '餐桌管理',
          path: 'OrderMealDesk',
        },
        {
          value: '3',
          text: '设置',
          path: 'OrderMealSetting',
        },
        {
          value: '4',
          text: '店铺点餐二维码',
          path: 'OrderMealQrcode',
        },
        {
          value: '5',
          text: '轮播图',
          path: 'OrderMealSlide',
        },
        {
          value: '6',
          text: '加料区',
          path: 'OrderMealAccessories',
        },
      ],
      total: 0,
      pageNo: 1,
      pageSize: 10,
      columns,

      queryParam: {
        name: '',
      },
      list: [],
      selectedRowKeys: [],
      visible: false,
      form: {
        name: '',
        desc: '',
        cover_pic: '',
        attr: [],
      },

      rules: {
        name: [{ required: true, message: '名称不能为空', trigger: 'blur' }],
      },
      labelCol: { span: 4 },
      wrapperCol: { span: 18 },
      showPreviwModal: false,
      previewImgSrc: '',
    }
  },

  watch: {
    visible(e) {
      if (!e) this.resetForm()
    },
  },

  mounted() {
    this.getList()
  },

  computed: {
    getTitle() {
      return this.form.id ? '编辑' : '新增'
    },

    getStatus() {
      return (status) => {
        let text, color
        switch (status) {
          case '0':
            text = '未审核'
            color = 'rgba(0, 0, 0, 0.25)'
            break

          case '1':
            text = '已通过'
            color = '#65C817'
            break

          case '2':
            text = '已拒绝'
            color = '#FF4D4F'
            break
        }
        return [text, color]
      }
    },
  },

  methods: {
    tabChange(e) {
      if (e == 6) return
      let item = this.tabs[e]
      this.$router.push({ name: item.path })
    },

    // 获取数据列表
    async getList(pageNo = 1) {
      const hide = this.$message.loading('加载中', 0)
      let { queryParam, pageSize } = this

      const res = await foodExtGoodsList({
        ...queryParam,
        pageSize,
        pageNo,
      })
      if (res.code == 0) {
        // this.selectedRowKeys = []
        let { totalCount, list: data } = res.data
        this.total = totalCount
        this.list = data

        console.log('data,', data)
      }
      hide()
    },

    // 分页请求
    tablePageChange(e) {
      this.getList(e)
    },

    addList() {
      this.form.attr.push({
        name: '',
        price: '',
        no: '',
        pic: '',
      })
    },

    previewPic(src) {
      this.previewImgSrc = src
      this.showPreviwModal = true
    },

    // 点击单个
    handleItem(type, item) {
      switch (type) {
        case 'remove':
          this.remove(item)
          break

        case 'edit':
          this.form = {
            id: item.id,
            name: item.name,
            desc: item.desc,
            cover_pic: item.cover_pic,
            attr: item.attr ? JSON.parse(item.attr) : [],
          }

          this.visible = true
          break
      }
    },

    handleOk() {
      this.$refs.ruleForm.validate(async (valid) => {
        if (valid) {
          let form = JSON.parse(JSON.stringify(this.form))

          if (!form.attr.every((el) => el.name)) {
            return this.$message.error('请完善小料名称～')
          }

          if (!form.attr.every((el) => el.price)) {
            return this.$message.error('请完善小料价格～')
          }

          this.disabled = true
          const hide = this.$message.loading('加载中', 0),
            res = await foodExtGoodsSave(form)

          hide()
          this.disabled = false

          if (res.code == 0) {
            this.$message.success('操作成功！')
            this.visible = false
            this.getList()
            return
          }
          this.$message.error(res.msg)
        } else {
          console.log('error submit!!')
          return false
        }
      })
    },

    remove({ id }) {
      this.$confirm({
        title: '提示?',
        content: '是否删除？',
        onOk: () => {
          foodExtGoodsDel({
            id,
          }).then((res) => {
            if (res.code !== 0) {
              this.$error({
                content: res.msg,
              })
            } else {
              this.$message.success(res.msg)
              this.getList()
            }
          })
        },
        onCancel: () => {},
      })
    },

    foodExtGoodsDefault({ id }) {
      foodExtGoodsDefault({
        id,
      }).then((res) => {
        if (res.code !== 0) {
          this.$error({
            content: res.msg,
          })
        } else {
          this.$message.success(res.msg)
          this.getList()
        }
      })
    },

    resetForm() {
      this.$refs.ruleForm.resetFields()
      this.form = {
        name: '',
        desc: '',
        cover_pic: '',
        attr: [],
      }
    },
  },
}
</script>

<style lang="less" scoped>
.searchBox {
  display: flex;
  align-items: center;
  flex-wrap: wrap;
  padding: 8px 0 18px 0;
  /deep/ .ant-input-affix-wrapper {
    width: 220px;
    margin-right: 20px;
  }
  .select {
    display: flex;
    align-items: center;
    margin-right: 20px;
  }
}

.content {
  display: flex;
  align-items: center;
  height: 48px;
  max-width: 300px;
  overflow: hidden;

  .txt {
    margin-left: 16px;
    flex: 1;
    overflow: hidden;

    * {
      margin: 0;
      font-size: 14px;
      text-overflow: ellipsis;
      white-space: nowrap;
      overflow: hidden;
    }

    h3 {
      color: rgba(0, 0, 0, 0.85);
    }

    p {
      color: rgba(0, 0, 0, 0.45);
    }
  }
}

.flex {
  .inp {
    margin-right: 16px;
  }
}

.table {
  width: 100%;
  margin-top: 10px;
  padding: 10px 12px;

  background: #ffffff;
  border: 1px solid rgba(0, 0, 0, 0.14901960784313725);
  opacity: 1;
  border-radius: 4px;
  .table-body {
    .ant-row {
      min-height: 54px;
      opacity: 1;
      border-radius: 4px 4px 0px 0px;
      display: flex;
      align-items: center;
      border-bottom: 1px solid #e8e8e8;
    }
    .self-th {
      background: #fafafa;
    }
  }
  .title {
    .ant-col {
      text-align: center;
      padding: 0 10px;
      font-size: 14px;
      font-family: PingFangSC-Medium;
      line-height: 22px;
      color: rgba(0, 0, 0, 0.85);
    }
  }
}
.list {
  .item {
    background-color: #fff;
    padding: 5px;
    display: flex;
    align-items: center;
    .ant-col {
      text-align: center;
      padding: 0 10px;
    }
  }
}
.tr-row:hover {
  background-color: #e6f7ff;
}

.upload-box {
  width: 50px;
  height: 50px;
  background: rgba(0, 0, 0, 0.05);
  border: 1px dashed rgba(0, 0, 0, 0.14901960784313725);
  opacity: 1;
  border-radius: 4px;
  position: relative;
  display: flex;
  justify-content: center;
  align-items: center;
  float: left;
  .upload-content {
    display: flex;
    justify-content: center;
    align-items: center;
    flex-direction: column;
  }
  .self-com {
    opacity: 0;
    width: 100%;
    height: 100%;
    position: absolute;
    left: 0;
    top: 0;
    z-index: 111;
    /deep/.ant-btn {
      width: 60px;
      height: 60px;
    }
    /deep/.ant-input-group-wrapper {
      width: 60px;
      height: 60px;
      .ant-input-group {
        width: 100%;
        height: 100%;
        span:nth-child(1) {
          display: none;
        }
        span:nth-child(3) {
          width: 100%;
          height: 100%;
          position: absolute;
          left: 0;
          top: 0;
        }
      }
    }
  }
}

.goods_pic_item_box {
  box-shadow: 0 0 5px rgba(170, 170, 170, 0.671);
  border-radius: 3px;
  position: relative;
  margin-right: 10px;
  width: 50px;
  height: 50px;
  background: rgba(0, 0, 0, 0.05);
  border: 1px solid rgba(0, 0, 0, 0.14901960784313725);
  border-radius: 4px;
  float: left;
  margin: 0 8px 0 0px;
  padding: 8px;
  display: flex;
  justify-content: center;
  align-items: center;
  .upload-img-box {
    width: 98%;
    height: 98%;
    position: relative;
    .action-hover {
      width: 100%;
      height: 100%;
      position: absolute;
      left: 0;
      top: 0;
      z-index: 11;
      display: none;
      opacity: 0;
      transition: all 0.3s;

      .hover-bg {
        width: 100%;
        height: 100%;
        background-color: #000000;
        opacity: 0.4;
      }
      .hover-body {
        width: 100%;
        height: 100%;
        position: absolute;
        left: 0;
        top: 0;
        z-index: 11;
        display: flex;
        justify-content: center;
        align-items: center;
      }
      .action-icons {
        display: flex;
        justify-content: center;
        align-items: center;
        padding: 5px;
        .anticon {
          color: #ffffff;
          font-size: 17px;
        }
        .anticon:nth-child(1) {
          margin-right: 10px;
        }
      }
    }
    img {
      width: 100%;
      height: 100%;
      display: block;
    }
  }
  .upload-img-box:hover > .action-hover {
    display: block;
    opacity: 1;
  }
}

.btn {
  text-align: center;
}
</style>
